<template>
  <view class="login-container">
    <view class="login-form">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>用户登录</span>
        </div>
        <el-form :model="loginForm" :rules="rules" ref="loginForm">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="loginForm.password" type="password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleLogin" style="width: 100%;">登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate( async (valid) => {
        if (valid) {
          // 登录成功后跳转到首页
          await this.$cloudOBjAjax('sunny-users', 'loginByAdmin', {
          	username: this.loginForm.username,
          	password: this.loginForm.password
          }).then(res=>{
              this.$store.commit('app/setUserInfo', res)
          uni.navigateTo({
            url:"/pages/home/home"
          })
          }).catch(err=>{
			  return this.$message({
				  type:'error',
				  message:err.errMsg
			  })
		  })
        
          
        } else {
          this.$message({
            message: '请输入完整的登录信息',
            type: 'warning'
          });
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: linear-gradient(225.16deg, #007BFF 0%, #36A0FF 100%);
}

.login-form {
  width: 400px;
}

.box-card {
  width: 100%;
}
</style>